package com.xjnt.frameworks.plugin.sql.criterion.expression;

import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.xjnt.frameworks.plugin.sql.criterion.Criterion;
import com.xjnt.frameworks.plugin.sql.criterion.SqlCriteria;
import com.xjnt.frameworks.plugin.sql.criterion.SqlType;
import com.xjnt.frameworks.plugin.sql.criterion.TypedValue;

public class SimpleExpression implements Criterion {
	
	private static final long serialVersionUID = 1L;

	private String columnName;
	private Object value;
	private String op;

	public SimpleExpression(String columnName, Object value, String op) {
		this.columnName = columnName;
		this.value = value;
		this.op = op;
	}

	@Override
	public String toSql(SqlCriteria criteria) {
		StringBuilder sql = new StringBuilder();
		if (criteria.isExists(columnName)) {
			sql.append(getColumnName());
			sql.append(" ").append(getOp());
			sql.append(" ").append("?");
			return sql.toString();
		} else {
			throw new ActiveRecordException("The "+ columnName + " is not exists ");
		}
	}
	
	@Override
	public TypedValue[] getTypedValues(SqlCriteria criterion) {
		return new TypedValue[]{ new SqlType().getTypedValue(criterion, columnName, value) };
	}

	public String getColumnName() {
		return columnName;
	}

	public Object getValue() {
		return value;
	}

	public String getOp() {
		return op;
	}
}
